#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/5/7 11:37
from typing import List

from sqlalchemy.sql.operators import is_
from sqlmodel import Session, select, or_

import common
from api.entity import Message


class MessageService(object):
    @staticmethod
    def create_messages(messages: List[Message], session: Session, autocommit: bool = True):
        try:
            session.add_all(messages)
            if autocommit:
                session.commit()
        except Exception as e:
            common.get_logger().error('创建会话报错：{}', e)
            session.rollback()

    @staticmethod
    def get_messages(conversation_id, session: Session):
        return session.exec(select(Message).where(Message.conversation_id == conversation_id).where(or_(Message.tag != 'tool_calls', is_(Message.tag, None))).order_by(Message.id)).all()